package com.david4.jizhang.util.controller;

import java.util.Calendar;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.david4.jizhang.common.BaseController;
import com.david4.jizhang.util.ResumeRefresh;

/**
 * 
 * @author <a href="mailto:hanxiaojun85@gmail.com">hanxiaojun</a>
 * @since 2010-5-19
 */
@Controller
public class RefreshResumeController extends BaseController {
	Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));

	@RequestMapping(value = "/resume.jhtml")
	public String resume(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// model.addAttribute("status", STATUS);
		return "util/resume";
	}

	@RequestMapping(value = "/refreshresume.jhtml")
	public String refresh(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		int h = calendar.get(Calendar.HOUR_OF_DAY);
		if (h > 8 && h < 20) {
			int begin = 0;
			int end = 40;
			int id = (int) (begin + (end - begin) * Math.random());
			try {
				Thread.sleep(1000 * id);
			} catch (Exception e) {
				logger.warn(e.getMessage());
			}
			try {
				ResumeRefresh.refresh51job();
				logger.warn("51job刷新成功");
			//	ResumeRefresh.refreshzhaopin();
			//	logger.warn("智联招聘刷新成功");
			} catch (Exception e) {
				logger.warn(e.getMessage());
			}
		}
		model.addAttribute(RESULT, "1");
		return RESULTPAGE;
	}

	@RequestMapping(value = "/stoprefresh.jhtml")
	public String stopRefresh(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		logger.warn("=========程序结束运行=========");
		// STATUS = false;
		model.addAttribute(RESULT, "1");
		return RESULTPAGE;
	}
}
